package com.sidianzhong.sdz.controller;

import com.alibaba.fastjson.JSONObject;
import com.sidianzhong.sdz.annotation.BackLoginToken;
import com.sidianzhong.sdz.annotation.UserLoginToken;
import com.sidianzhong.sdz.model.*;
import com.sidianzhong.sdz.service.*;
import com.sidianzhong.sdz.service.commond.RedisService;
import com.sidianzhong.sdz.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import java.util.stream.Collectors;

/**
 * Created by hxg on 2016/10/06.
 */
@Api(description = "用户")
@Controller
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    HttpServletRequest request;
    @Autowired
    HttpServletResponse response;

    @UserLoginToken
    @ApiOperation(value = "创建'用户'表中一条信息")
    @RequestMapping(value = "/user/new", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object createUser(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "userName", required = false) String userName,
            @RequestParam(value = "headPortrait", required = false) String headPortrait,
            @RequestParam(value = "invitationCode", required = false) String invitationCode,
            @RequestParam(value = "inviter", required = false) Integer inviter,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "area", required = false) String area,
            @RequestParam(value = "loginPassword", required = false) String loginPassword,
            @RequestParam(value = "payPassword", required = false) String payPassword,
            @RequestParam(value = "loginNum", required = false) Integer loginNum,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "createTime", required = false) String createTime,
            @RequestParam(value = "lastUpdateTime", required = false) String lastUpdateTime
    ) {
        User item = new User();
        Date date = new Date();
        if( userName != null ){
            item.setUserName(userName);
        }
        if( headPortrait != null ){
            item.setHeadPortrait(headPortrait);
        }
        if( invitationCode != null ){
            item.setInvitationCode(invitationCode);
        }
        if( inviter != null ){
            item.setInviter(inviter);
        }
        if( phone != null ){
            item.setPhone(phone);
        }
        if( area != null ){
            item.setArea(area);
        }
        if( loginPassword != null ){
            item.setLoginPassword(loginPassword);
        }
        if( payPassword != null ){
            item.setPayPassword(payPassword);
        }
        if( loginNum != null ){
            item.setLoginNum(loginNum);
        }
        if( status != null ){
            item.setStatus(status);
        }
            item.setCreateTime(new Date());
            item.setLastUpdateTime(new Date());
        User iteams = userService.create(item);
        boolean result = iteams != null;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        return new ResponseEntity<>(ResultModel.ok(jsonObject), HttpStatus.OK);
    }
    @UserLoginToken
    @ApiOperation(value = "删除'用户'表中的某条记录")
    @RequestMapping(value = "/user/delete",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object deleteUser(
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "id") Integer id
    ) {
        User item = userService.get(id);
        if (null == item) {
            return new ResponseEntity<>(ResultModel.error(), HttpStatus.OK);
        }
        int delete = userService.delete(id);
        boolean result = delete!=0;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        return new ResponseEntity<>(ResultModel.ok(jsonObject), HttpStatus.OK);
    }
    @UserLoginToken
    @ApiOperation(value = "修改'用户'表中的某条记录")
    @RequestMapping(value = "/user/edit",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object editUser(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "id") Integer id,
            @RequestParam(value = "userName", required = false) String userName,
            @RequestParam(value = "headPortrait", required = false) String headPortrait,
            @RequestParam(value = "invitationCode", required = false) String invitationCode,
            @RequestParam(value = "inviter", required = false) Integer inviter,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "area", required = false) String area,
            @RequestParam(value = "loginPassword", required = false) String loginPassword,
            @RequestParam(value = "payPassword", required = false) String payPassword,
            @RequestParam(value = "loginNum", required = false) Integer loginNum,
            @RequestParam(value = "status", required = false) Integer status
    ) {
        User item = userService.get(id);
        if (null == item) {
           return new ResponseEntity<>(ResultModel.error(), HttpStatus.OK);
        }
        if( userName != null ){
            item.setUserName(userName);
        }
        if( headPortrait != null ){
            item.setHeadPortrait(headPortrait);
        }
        if( invitationCode != null ){
            item.setInvitationCode(invitationCode);
        }
        if( inviter != null ){
            item.setInviter(inviter);
        }
        if( phone != null ){
            item.setPhone(phone);
        }
        if( area != null ){
            item.setArea(area);
        }
        if( loginPassword != null ){
            item.setLoginPassword(loginPassword);
        }
        if( payPassword != null ){
            item.setPayPassword(payPassword);
        }
        if( loginNum != null ){
            item.setLoginNum(loginNum);
        }
        if( status != null ){
            item.setStatus(status);
        }
            item.setLastUpdateTime(new Date());
        int update =userService.update(item);
        boolean result = update!=0;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        return new ResponseEntity<>(ResultModel.ok(jsonObject), HttpStatus.OK);
    }
    @UserLoginToken
    @ApiOperation(value = "查询'用户'表中的某条记录")
    @RequestMapping(value = "/getUserById",method = {RequestMethod.GET},produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getUserById(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token") String token,
            @RequestParam(value = "id") Integer id
    ) {
            User item = userService.get(id);
            if (null == item) {
                return new ResponseEntity<>(ResultModel.error(), HttpStatus.OK);
            }
             return new ResponseEntity<>(ResultModel.ok(item), HttpStatus.OK);
    }
    @UserLoginToken
    @ApiOperation(value = "查询'用户'表中的多条记录返回对象")
    @RequestMapping(value = "/users",method = { RequestMethod.GET },produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getUsers(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token", required = false) String token,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortItem", required = false, defaultValue = "id") String sortItem,
            @RequestParam(value = "sortOrder", required = false, defaultValue = "desc") String sortOrder,
            @RequestParam(value = "userName", required = false) String userName,
            @RequestParam(value = "headPortrait", required = false) String headPortrait,
            @RequestParam(value = "invitationCode", required = false) String invitationCode,
            @RequestParam(value = "inviter", required = false) Integer inviter,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "area", required = false) String area,
            @RequestParam(value = "loginPassword", required = false) String loginPassword,
            @RequestParam(value = "payPassword", required = false) String payPassword,
            @RequestParam(value = "loginNum", required = false) Integer loginNum,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "createTime", required = false) String createTime,
            @RequestParam(value = "lastUpdateTime", required = false) String lastUpdateTime
    ) {
            User item = new User();
            if( userName != null ){
                item.setUserName(userName);
            }
            if( headPortrait != null ){
                item.setHeadPortrait(headPortrait);
            }
            if( invitationCode != null ){
                item.setInvitationCode(invitationCode);
            }
            if( inviter != null ){
                item.setInviter(inviter);
            }
            if( phone != null ){
                item.setPhone(phone);
            }
            if( area != null ){
                item.setArea(area);
            }
            if( loginPassword != null ){
                item.setLoginPassword(loginPassword);
            }
            if( payPassword != null ){
                item.setPayPassword(payPassword);
            }
            if( loginNum != null ){
                item.setLoginNum(loginNum);
            }
            if( status != null ){
                item.setStatus(status);
            }
            if( createTime != null ){
                item.setCreateTime(Tools.convertStringToDate(createTime, "yyyy-MM-dd HH:mm:ss"));
            }
            if( lastUpdateTime != null ){
                item.setLastUpdateTime(Tools.convertStringToDate(lastUpdateTime, "yyyy-MM-dd HH:mm:ss"));
            }
       PageInfo<User> list =  userService.getListWithPaging(pageNum, pageSize, sortItem, sortOrder,item);
       return new ResponseEntity<>(ResultModel.ok(list), HttpStatus.OK);
    }
    @UserLoginToken
    @ApiOperation(value = "查询'用户'表中的多条记录返回自定义JSONObject")
    @RequestMapping(value = "/usersObject",method = { RequestMethod.GET },produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getUsersObject(
            HttpServletRequest request,
            @RequestHeader(value = "X-Auth-Token", required = false) String token,
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "sortItem", required = false, defaultValue = "id") String sortItem,
            @RequestParam(value = "sortOrder", required = false, defaultValue = "desc") String sortOrder,
            @RequestParam(value = "userName", required = false) String userName,
            @RequestParam(value = "headPortrait", required = false) String headPortrait,
            @RequestParam(value = "invitationCode", required = false) String invitationCode,
            @RequestParam(value = "inviter", required = false) Integer inviter,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "area", required = false) String area,
            @RequestParam(value = "loginPassword", required = false) String loginPassword,
            @RequestParam(value = "payPassword", required = false) String payPassword,
            @RequestParam(value = "loginNum", required = false) Integer loginNum,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "createTime", required = false) String createTime,
            @RequestParam(value = "lastUpdateTime", required = false) String lastUpdateTime
    ) {
           User item = new User();
           if( userName != null ){
                item.setUserName(userName);
            }
            if( headPortrait != null ){
                item.setHeadPortrait(headPortrait);
            }
            if( invitationCode != null ){
                item.setInvitationCode(invitationCode);
            }
            if( inviter != null ){
                item.setInviter(inviter);
            }
            if( phone != null ){
                item.setPhone(phone);
            }
            if( area != null ){
                item.setArea(area);
            }
            if( loginPassword != null ){
                item.setLoginPassword(loginPassword);
            }
            if( payPassword != null ){
                item.setPayPassword(payPassword);
            }
            if( loginNum != null ){
                item.setLoginNum(loginNum);
            }
            if( status != null ){
                item.setStatus(status);
            }
            if( createTime != null ){
                item.setCreateTime(Tools.convertStringToDate(createTime, "yyyy-MM-dd HH:mm:ss"));
            }
            if( lastUpdateTime != null ){
                item.setLastUpdateTime(Tools.convertStringToDate(lastUpdateTime, "yyyy-MM-dd HH:mm:ss"));
            }
           PageInfo<JSONObject> list  =  userService.getListWithObject(pageNum, pageSize, sortItem, sortOrder,item);
           List<JSONObject> list1 = list.getList();
           List<JSONObject> collect = list1.stream().map(ls -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id",ls.getInteger("id"));
            jsonObject.put("userName",ls.getString("userName"));
            jsonObject.put("headPortrait",ls.getString("headPortrait"));
            jsonObject.put("invitationCode",ls.getString("invitationCode"));
            jsonObject.put("inviter",ls.getInteger("inviter"));
            jsonObject.put("phone",ls.getString("phone"));
            jsonObject.put("area",ls.getString("area"));
            jsonObject.put("loginPassword",ls.getString("loginPassword"));
            jsonObject.put("payPassword",ls.getString("payPassword"));
            jsonObject.put("loginNum",ls.getInteger("loginNum"));
            jsonObject.put("status",ls.getInteger("status"));
            jsonObject.put("createTime",ls.getDate("createTime"));
            jsonObject.put("lastUpdateTime",ls.getDate("lastUpdateTime"));
            return jsonObject;
           }).collect(Collectors.toList());
           list.setList(collect);
           return new ResponseEntity<>(ResultModel.ok(list), HttpStatus.OK);
        }
}
